package cn.shujuhai.workflow.controller.form;


import cn.shujuhai.common.domain.PageRequest;
import cn.shujuhai.common.pluging.data.ResultVo;
import cn.shujuhai.workflow.domain.Form;
import cn.shujuhai.workflow.service.FormService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @Author: ZhangDeZhong
 * @Date: 2025/3/11 09:42
 * @Description: 提供表【form_config(表单结构)】的操作接口
 **/
@RestController
@RequestMapping("/workflow/form")
@RequiredArgsConstructor
public class FormController {

    final FormService formService;

    /**
     * 根据分页查询参数获取表单列表
     *
     * @param formName    搜索关键字（表单名称）
     * @param pageRequest
     * @return 表单列表的分页结果
     */
    @GetMapping("/list")
    public ResultVo<?> getFormList(
            @RequestParam(defaultValue = "") String formName,
            @RequestParam(defaultValue = "") Integer formType,
            @Valid PageRequest pageRequest) {
        return this.formService.getFormList(formName, formType, pageRequest.getPageNum(), pageRequest.getPageSize());
    }

    /**
     * 添加表单信息
     *
     * @param form 表单信息
     * @return 结果信息
     */
    @PostMapping("/add")
    public ResultVo<?> addForm(@RequestBody @Validated Form form) {
        return this.formService.addForm(form);
    }

    /**
     * 修改表单信息
     *
     * @param form 表单信息
     * @return 结果信息
     */
    @PostMapping("/update")
    public ResultVo<?> updateForm(@RequestBody @Validated Form form) {
        return this.formService.updateForm(form);
    }


    /**
     * 删除表单信息
     *
     * @param formId 表单ID
     * @return 结果信息
     */
    @PostMapping("/delete/{formId}")
    public ResultVo<?> deleteForm(@PathVariable Long formId) {
        return this.formService.deleteForm(formId);
    }

    /**
     * 启用表单信息
     *
     * @param formId 表单ID
     * @return 结果信息
     */
    @PostMapping("/enable/{formId}")
    public ResultVo<?> enableForm(@PathVariable Long formId) {
        return this.formService.enableForm(formId);
    }

    /**
     * 查看表单是否被填写
     *
     * @param formId 表单ID
     * @return 结果信息
     */
    @GetMapping("/isFormFilled")
    public ResultVo<?> isFormFilled(@RequestParam Long formId) {
        return ResultVo.success(this.formService.isFormFilled(formId));
    }


    /**
     * 根据ID获取表单
     *
     * @param formId 表单ID
     * @return 结果信息
     */
    @GetMapping("/getFormById")
    public ResultVo<?> getFormById(@RequestParam Long formId) {
        return this.formService.getFormById(formId);
    }

    /**
     * 表单背景图片上传
     *
     * @param file 文件内容
     * @return 结果信息
     */
    @PostMapping("/uploadImg")
    public ResultVo<?> uploadImg(@RequestParam("file") MultipartFile file) throws IOException {
        String s = this.formService.uploadImg(file);
        return ResultVo.success(s);
    }

    /**
     * 表单背景图保存
     *
     * @param formId 表单Id
     * @return 结果信息
     */
    @PostMapping("/saveFormStyle")
    public ResultVo<?> saveFormStyle(@RequestParam Long formId, String background) {
        return this.formService.saveFormStyle(formId, background);
    }

    /**
     * 根据表单id查表单fieldData
     *
     * @param formId 表单Id
     * @return 结果信息
     */
    @GetMapping("/getFieldDataByFormId")
    public ResultVo<?> getFieldDataByFormId(@RequestParam Long formId) {
        return this.formService.getFieldDataByFormId(formId);
    }


}
